package top.jandmla.excel.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.jandmla.excel.pojo.DownloadFileDTO;
import top.jandmla.excel.service.FileDownLoadService;
import top.jandmla.excel.service.NameService;
import top.jandmla.excel.util.result.BaseResult;

import javax.servlet.http.HttpServletResponse;

@RestController
public class FileController {
    @Autowired
    private FileDownLoadService fileService;

    @Autowired
    private NameService nameService;

    /**
     * 下载文件
     *
     * @param dto 下载请求
     * @param rse HttpServletResponse请求对象
     */
    @PostMapping("downloadFile")
    public void downloadFileGet(@RequestBody DownloadFileDTO dto, HttpServletResponse rse) {
        fileService.downLoadFile(dto, rse);
    }

    /**
     * 上传文件操作
     *
     * @param file 上传的文件
     * @return 上传文件的结果字符串
     */
    @GetMapping(value = "/upload")
    public BaseResult<String> upload(@RequestParam("file") MultipartFile file) {
        if (file == null)
            return BaseResult.fail(1, "上传失败！请选择文件");
        return nameService.insertFile(file) ? BaseResult.success("上传成功！") : BaseResult.fail(1, "上传失败！");
    }

}
